local dimeng = fk.CreateSkill {

  name = "joyex__dimeng",

  tags = {  },

}



dimeng:addEffect("active", {
  name = "joyex__dimeng",
  anim_type = "control",
  card_num = 0,
  target_num = 2,
  prompt = "#joyex__dimeng",
  can_use = function(self, player)
    return player:usedSkillTimes(dimeng.name) == 0 and player:getMark("@"..dimeng.name) == 0
  end,
  card_filter = function(self, to_select, selected, selected_targets)
    return false
  end,
  target_filter = function(self, player, to_select, selected)
    if #selected > 1 then return false end
    if #selected == 0 then
      return true
    else
      local target1 = Fk:currentRoom():getPlayerById(to_select)
      local target2 = Fk:currentRoom():getPlayerById(selected[1])
      local num, num2 = target1:getHandcardNum(), target2:getHandcardNum()
      if num == 0 and num2 == 0 then
        return false
      end
      return true
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target1 = effect.tos[1]
    local target2 = room:getPlayerById(effect.tos[2])
    local num = math.abs(target1:getHandcardNum() - target2:getHandcardNum())
    room:addPlayerMark(player,"@"..dimeng.name,num)
    U.swapHandCards(room, player, target1, target2, dimeng.name)
  end,
})

dimeng:addEffect(fk.EventPhaseEnd, {
  name = "#joyex__dimeng_delay",
  events = {fk.EventPhaseEnd,fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(dimeng.name) and player:getMark("@joyex__dimeng") > 0 and not player:isNude() and (player.phase == Player.Draw or event == fk.TurnEnd) 
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askForCard(player,1,player:getMark("@joyex__dimeng"),true,dimeng.name,true,".","缔盟：你可以弃置任意张牌并移除等量“盟”标记。")
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(event:getCostData(self),dimeng.name,player,player)
    if not player.dead then
      player.room:removePlayerMark(player,"@joyex__dimeng",#event:getCostData(self))
    end
  end,
})
dimeng:addEffect(fk.TurnEnd, {
  name = "#joyex__dimeng_delay",
  events = {fk.EventPhaseEnd,fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(dimeng.name) and player:getMark("@joyex__dimeng") > 0 and not player:isNude() and (player.phase == Player.Draw or event == fk.TurnEnd) 
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askForCard(player,1,player:getMark("@joyex__dimeng"),true,dimeng.name,true,".","缔盟：你可以弃置任意张牌并移除等量“盟”标记。")
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(event:getCostData(self),dimeng.name,player,player)
    if not player.dead then
      player.room:removePlayerMark(player,"@joyex__dimeng",#event:getCostData(self))
    end
  end,
})

return dimeng